package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsCategoryMapper;
import com.ruoyi.mall.domain.PmsCategory;
import com.ruoyi.mall.service.IPmsCategoryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品类别Service业务层处理
 *
 * @author zhanghang
 * @date 2023-11-20
 */
@Service
public class PmsCategoryServiceImpl implements IPmsCategoryService {
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    /**
     * 查询商品类别
     *
     * @param id 商品类别主键
     * @return 商品类别
     */
    @Override
    public PmsCategory selectPmsCategoryById(Long id) {
        return pmsCategoryMapper.selectPmsCategoryById(id);
    }

    /**
     * 查询商品类别列表
     *
     * @param pmsCategory 商品类别
     * @return 商品类别
     */
    @Override
    public List<PmsCategory> selectPmsCategoryList(PmsCategory pmsCategory) {
        return pmsCategoryMapper.selectPmsCategoryList(pmsCategory);
    }

    /**
     * 新增商品类别
     *
     * @param pmsCategory 商品类别
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPmsCategory(PmsCategory pmsCategory) {
        String pmsCategoryName = pmsCategory.getName();
        if (StringUtils.isEmpty(pmsCategoryName)){
            throw new RuntimeException("分类名称不能为空");
        }
        if(pmsCategoryMapper.selectPmsCategoryByName(pmsCategoryName)!=null){
            throw new RuntimeException("分类名称已存在");
        }
        Long pmsCategoryParentId = pmsCategory.getParentId();
        PmsCategory selectPmsCategoryByParentId = pmsCategoryMapper.selectPmsCategoryByParentId(pmsCategoryParentId);
        if (pmsCategoryParentId != 0 && selectPmsCategoryByParentId==null){
            throw new RuntimeException("请选择上级分类或上级分类不存在");
        }else if (pmsCategoryParentId == 0){
            pmsCategory.setParentId(0L);
        }

        Long depth = 1L;
        while (true){
            PmsCategory pmsCategoryByParentId = pmsCategoryMapper.selectPmsCategoryByParentId(pmsCategoryParentId);
            if(pmsCategoryByParentId!=null){
                depth++;
                pmsCategoryParentId = pmsCategoryByParentId.getParentId();
            }else {
                break;
            }
        }
        pmsCategory.setDepth(depth);

        if (pmsCategoryParentId != 0){
            Long pmsCategoryId = selectPmsCategoryByParentId.getId()+1;
            if(pmsCategoryMapper.selectParentIdById(pmsCategoryId).isEmpty()){
                pmsCategory.setIsParent(0);
            }else {
                pmsCategory.setIsParent(1);
            }
        }else {
            pmsCategory.setIsParent(0);
        }

        Date date = new Date();
        pmsCategory.setGmtModified(date);
        pmsCategory.setGmtCreate(date);

        int inserted = pmsCategoryMapper.insertPmsCategory(pmsCategory);
        if (inserted>0){
            PmsCategory selectPmsCategoryById = pmsCategoryMapper.selectPmsCategoryById(pmsCategory.getParentId());
            if (selectPmsCategoryById!=null){
                pmsCategoryMapper.updatePmsCategoryById(selectPmsCategoryById.getId());
            }
            return inserted;
        }else {
            return 0;
        }
    }

    /**
     * 修改商品类别
     *
     * @param pmsCategory 商品类别
     * @return 结果
     */
    @Override
    public int updatePmsCategory(PmsCategory pmsCategory) {
        String pmsCategoryName = pmsCategory.getName();
        if (StringUtils.isEmpty(pmsCategoryName)){
            throw new RuntimeException("分类名称不能为空");
        }
        PmsCategory pmsCategoryByName = pmsCategoryMapper.selectPmsCategoryByName(pmsCategoryName);
        if(pmsCategoryByName !=null && !pmsCategory.getId().equals(pmsCategoryByName.getId())){
            throw new RuntimeException("分类名称已存在");
        }
        pmsCategory.setGmtModified(new Date());
        return pmsCategoryMapper.updatePmsCategory(pmsCategory);
    }


    /**
     * 删除商品类别信息
     *
     * @param id 商品类别主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePmsCategoryById(Long id) {
        PmsCategory pmsCategory = pmsCategoryMapper.selectPmsCategoryById(id);
        if (pmsCategory==null){
            throw new RuntimeException("该分类不存在");
        }
        if (pmsCategory.getIsParent()==1){
            throw new RuntimeException("该分类下有子分类，不允许删除");
        }
        Long pmsCategoryParentId = pmsCategory.getParentId();
        int deleted = pmsCategoryMapper.deletePmsCategoryById(id);
        if (deleted>0){
            List<PmsCategory> selectParentIdById = pmsCategoryMapper.selectParentIdById(pmsCategoryParentId);
            if (selectParentIdById.isEmpty()){
                PmsCategory pmsCategory1 = new PmsCategory();
                pmsCategory1.setIsParent(0);
                pmsCategory1.setGmtModified(new Date());
                pmsCategory1.setId(pmsCategoryParentId);
                pmsCategoryMapper.updatePmsCategory(pmsCategory1);
            }
            return deleted;
        }
        return 0;
    }
}
